package com.lemon.smartsafetynew.net.services;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.BaseHttpServices;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolCheckItemSetVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolChildItemHistoryVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolHistoryItemVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskVo;
import com.lemon.smartsafetynew.entity.task.InspectionDetailVo;
import com.lemon.smartsafetynew.entity.task.InspectionIndexVo;
import com.lemon.smartsafetynew.entity.task.InspectionRecordVo;
import com.lemon.smartsafetynew.entity.task.InspectionVo;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew on 2017/7/31.
 */

public class SafePatrolServices extends BaseHttpServices {

    public static final String TAG = "SafePatrolServices";

    public SafePatrolServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static SafePatrolServices init() {
        return new SafePatrolServices(IApplication.context());
    }


    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public static SafePatrolServices init(Context context) {
        return new SafePatrolServices(context);
    }

    /**
     * 获取安全巡查任务列表
     *
     * @param type
     * @param params { token, f_state, date, pageNum, pageSize }
     * @param status { to_do("1", "待执行"), doing("2", "执行中"), done("3", "已完成"), over_time("4", "已超时")}
     */
    public void getSafePatrolTaskList(RefreshType type, LemonRequestParams params, String status) {

        HandlerCallback<List<SafePatrolTaskVo>> callback = new HandlerCallback<List<SafePatrolTaskVo>>(KeyConstants.URL_GET_SAFE_PATROL_TASK_LIST + status) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_TASK_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);
    }

    /**
     * 开始任务
     */
    public void startPatrolTask(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_START_PATROL_TASK) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.START_PATROL_TASK, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 结束任务
     */
    public void finishPatrolTask(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_FINISH_PATROL_TASK) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.FINISH_PATROL_TASK, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取安全巡查任务的检查项列表
     */
    public void getSafePatrolTask(RefreshType type, LemonRequestParams params) {

        HandlerCallback<List<SafePatrolTaskItemVo>> callback = new HandlerCallback<List<SafePatrolTaskItemVo>>(KeyConstants.URL_GET_SAFE_PATROL_TASK) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_TASK, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取安全巡查任务的检查项子项列表
     */
    public void getSafePatrolCheckItemList(LemonRequestParams params) {

        HandlerCallback<SafePatrolCheckItemSetVo> callback = new HandlerCallback<SafePatrolCheckItemSetVo>(KeyConstants.URL_GET_SAFE_PATROL_CHECK_ITEM_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_CHECK_ITEM_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /***
     * 提交检查项巡查结果
     * @param params
     * @param fileMap
     */
    public void submitSafePatrolItemCheckResult(LemonRequestParams params, Map<String, File> fileMap) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_SUBMIT_SAFE_PATROL_CHECK_ITEM_RESULT) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.SUBMIT_SAFE_PATROL_CHECK_ITEM_RESULT, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .addFiles("files", fileMap)
                .build().execute(callback);

    }

    /***
     * 提交检查项巡查结果无附件
     * @param params
     */
    public void submitSafePatrolItemCheckResultNoFile(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_SUBMIT_SAFE_PATROL_CHECK_ITEM_RESULT_NO_FILE) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.SUBMIT_SAFE_PATROL_CHECK_ITEM_RESULT_NO_FILE, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /***
     * 提交检查子项的检查结果
     * @param params
     * @param fileMap
     */
    public void submitSafePatrolChildItemResult(LemonRequestParams params, Map<String, File> fileMap) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_SUBMIT_SAFE_PATROL_CHILD_ITEM_RESULT) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.SUBMIT_SAFE_PATROL_CHILD_ITEM_RESULT, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .addFiles("files", fileMap)
                .build().execute(callback);

    }

    /**
     * 获取检查项的历史检查结果
     */
    public void getSafePatrolCheckItemHistoryList(LemonRequestParams params) {

        HandlerCallback<List<SafePatrolHistoryItemVo>> callback = new HandlerCallback<List<SafePatrolHistoryItemVo>>(KeyConstants.URL_GET_SAFE_PATROL_CHECK_ITEM_HISTORY_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_CHECK_ITEM_HISTORY_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取检查项的历史检查结果
     * 分页版，不用日期，返回全部
     */
    public void getSafePatrolCheckItemHistoryList(RefreshType type, LemonRequestParams params) {

        HandlerCallback<List<SafePatrolHistoryItemVo>> callback = new HandlerCallback<List<SafePatrolHistoryItemVo>>(KeyConstants.URL_GET_SAFE_PATROL_CHECK_ITEM_HISTORY_LIST) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_CHECK_ITEM_HISTORY_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取检查项子项的历史检查结果
     */
    public void getSafePatrolChildItemHistoryList(LemonRequestParams params) {

        HandlerCallback<SafePatrolChildItemHistoryVo> callback = new HandlerCallback<SafePatrolChildItemHistoryVo>(KeyConstants.URL_GET_SAFE_PATROL_CHILD_ITEM_HISTORY_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_CHILD_ITEM_HISTORY_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);

    }

    /**
     * 通过二维码或者NFC获取检查点信息
     * @params   企业id，用户角色，二维码和NFC获取检查点
     */
    public void getSafePatrolItem(LemonRequestParams params) {

        HandlerCallback<SafePatrolTaskItemVo> callback = new HandlerCallback<SafePatrolTaskItemVo>(KeyConstants.URL_GET_SAFE_PATROL_ITEM) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_ITEM, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /**
     * 通过二维码或者NFC获取检查点信息
     * @params   企业id，用户角色，二维码和NFC获取检查点
     */
    public void getSafePatrolInspectionItem(LemonRequestParams params) {

        HandlerCallback<SafePatrolTaskItemVo> callback = new HandlerCallback<SafePatrolTaskItemVo>(KeyConstants.URL_GET_INSPECTION_SAFE_PATROL_ITEM) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_PATROL_ITEM, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取巡查任务列表
     { to_do("1", "待执行"), doing("2", "执行中"), done("3", "已完成"), over_time("4", "已超时")}
     */
    public void getInspectionTaskList(RefreshType type, LemonRequestParams params, String status) {
        HandlerCallback<List<InspectionVo>> callback = new HandlerCallback<List<InspectionVo>>(KeyConstants.URL_GET_INSPECTION_TASK_LIST + status) {
        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_INSPECTION_TASK_LIST, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);
    }

    /**
     * 获取巡查任务列表的数字上标
     */
    public void getInspectionIndex(LemonRequestParams params) {

        HandlerCallback<InspectionIndexVo> callback = new HandlerCallback<InspectionIndexVo>(KeyConstants.URL_GET_INSPECTION_INDEX) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_INSPECTION_INDEX, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取巡查任务列表任务详情
     */
    public void getInspectionListDetail(LemonRequestParams params) {

        HandlerCallback<InspectionDetailVo> callback = new HandlerCallback<InspectionDetailVo>(KeyConstants.URL_GET_INSPECTION_TASK_DETAIL) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_INSPECTION_TASK_DETAIL, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /**
     * 获取巡查任务列表任务详情检查记录
     */
    public void getInspectionListPatrol(LemonRequestParams params) {

        HandlerCallback<List<InspectionRecordVo>> callback = new HandlerCallback<List<InspectionRecordVo>>(KeyConstants.URL_GET_INSPECTION_TASK_PARTROL_RECORD) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_INSPECTION_TASK_PARTROL_RECORD, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }

    /**
     * 完成巡查任务
     */
    public void getFinishInspection(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_GET_FINISH_INSPECTION_TASK) {
        };

        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_FINISH_INSPECTION_TASK, Constants.SERVER_V2);
        OkHttpUtils.post()
                .url(serverUrl)
                .tag(callback.getTag())
                .params(params)
                .build().execute(callback);

    }
}
